/**
 * 版权所有 2009-2012山东新北洋信息技术股份有限公司
 * 保留所有权利。
 */
package com.linyaonan.leetcode.easy._111;


/**
 * @ProjectName: leetcode
 * @Package: com.linyaonan.leetcode.easy._111
 * @ClassName: MinDepthBinaryTree
 * @Author: linyaonan
 * @Date: 2019/11/4 11:18
 */
public class MinDepthBinaryTree {

    int nodeTreeMin = -1;

    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        getTreeNodeDepth(root.left, root.right, 1);
        return nodeTreeMin;
    }

    private void getTreeNodeDepth(TreeNode leftNode, TreeNode rightNode, int count) {
        if (leftNode == null && rightNode == null) {
            if (nodeTreeMin == -1) {
                nodeTreeMin = count;
            } else {
                nodeTreeMin = Math.min(nodeTreeMin, count);
            }
            return;
        }
        count++;
        if (leftNode == null) {
            getTreeNodeDepth(rightNode.left, rightNode.right, count);
            return;
        }
        if (rightNode == null) {
            getTreeNodeDepth(leftNode.left, leftNode.right, count);
            return;
        }
        getTreeNodeDepth(leftNode.left, leftNode.right, count);
        getTreeNodeDepth(rightNode.left, rightNode.right, count);

    }

}
